package com.hw.one.dam2.dao;

import com.hw.one.dam2.param.CityAirParam;
import com.hw.one.dam2.po.RegionAirQualityPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/06/27 11:31.
 */
@Repository(value = "regionAirDao")
public class RegionAirDao implements IRegionAirDao {

    @Autowired
    private MongoTemplate mongoTemplateStandardSiteData;

    private static String REGION_AIR_HOUR = "T_DATA_REGION_AIR_HOUR";
    private static String REGION_AIR_DAY = "T_DATA_REGION_AIR_DAY";
    private static String REGION_AIR_MONTH = "T_DATA_REGION_AIR_MONTH";
    private static String REGION_AIR_YEAR = "T_DATA_REGION_AIR_YEAR";

    @Override
    public List<RegionAirQualityPO> findRegionHourDataByRegionCodesAndTime(CityAirParam cityAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
            .lte(cityAirParam.getEndTime());
        criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        List<RegionAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_HOUR + "-" + cityAirParam.getEndTime().substring(0, 4));
        return resultList;
    }

    @Override
    public List<RegionAirQualityPO> findRegionRealHourDataByRegionCodes(CityAirParam cityAirParam) {
        // 查询条件
        Criteria criteria = Criteria.where("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        List<RegionAirQualityPO> resultList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_HOUR);
        return resultList;
    }

    @Override
    public List<RegionAirQualityPO> findRegionHourDataByRegionCodes(List<String> regionCodeList) {
        String tableName = REGION_AIR_HOUR;
        Query query = new Query().with(new Sort(Sort.Direction.DESC, "MONITORTIME"));
        RegionAirQualityPO cityAQIInfo = mongoTemplateStandardSiteData.findOne(query, RegionAirQualityPO.class, tableName);
        // 得到最新时间
        Object timeObj = cityAQIInfo.getMONITORTIME();

        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").is(timeObj);
        criteria.and("REGIONCODE").in(regionCodeList);
        // 创建查询对象
        Query query1 = new Query(criteria);

        List<RegionAirQualityPO> cityNewDataList = mongoTemplateStandardSiteData.find(query1, RegionAirQualityPO.class, tableName);

        Collections.sort(cityNewDataList, (a, b) -> {
            if (null == a.getAQI() && null == b.getAQI()) {
                return 0;
            }
            if (null == a.getAQI() || "-".equals(a.getAQI())) {
                return 1;
            }
            if (null == b.getAQI() || "-".equals(b.getAQI())) {
                return -1;
            }
            Double one = Double.valueOf(a.getAQI());
            Double two = Double.valueOf(b.getAQI());
            return (int) (two - one);
        });
        return cityNewDataList;
    }

    @Override
    public List<RegionAirQualityPO> findRegionDayDataByRegionCodesAndTime(CityAirParam cityAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
            .lte(cityAirParam.getEndTime());
        criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        // 查询数据 日数据
        List<RegionAirQualityPO> cityDayAirList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_DAY + "-" + cityAirParam.getEndTime().substring(0, 4));

        return cityDayAirList;
    }

    @Override
    public List<RegionAirQualityPO> findRegionMonthDataByRegionCodesAndTime(CityAirParam cityAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
            .lte(cityAirParam.getEndTime());
        criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        // 查询数据 日数据
        List<RegionAirQualityPO> regionMonthAirList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_MONTH + "-" + cityAirParam.getEndTime().substring(0, 4));

        return regionMonthAirList;
    }

    @Override
    public List<RegionAirQualityPO> findRegionYearDataByRegionCodesAndTime(CityAirParam cityAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
            .lte(cityAirParam.getEndTime());
        criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME").and(new Sort(Sort.Direction.DESC, "COMPOSITEVALUE")));
        // 查询数据 日数据
        List<RegionAirQualityPO> regionYearAirList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_YEAR + "-" + cityAirParam.getEndTime().substring(0, 4));
        return regionYearAirList;
    }

    @Override
    public RegionAirQualityPO findRegionMonthDataByRegionCodeAndTime(CityAirParam cityAirParam) {
        Criteria criteria = Criteria.where("REGIONCODE").is(cityAirParam.getRegionCodes().get(0));
        criteria.and("MONITORTIME").is(cityAirParam.getEndTime());
        // 创建查询对象
        Query query = new Query(criteria);
        // 查询数据
        RegionAirQualityPO resultMap = mongoTemplateStandardSiteData.findOne(query, RegionAirQualityPO.class,
            REGION_AIR_MONTH + "-" + cityAirParam.getEndTime().substring(0, 4));
        return resultMap;
    }

    @Override
    public List<RegionAirQualityPO> findRegionDayDataByRegionCodesAndTimeASC(CityAirParam cityAirParam) {
        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("MONITORTIME").gte(cityAirParam.getStartTime())
            .lte(cityAirParam.getEndTime());
        criteria.and("REGIONCODE").in(cityAirParam.getRegionCodes());

        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.ASC, "MONITORTIME"));
        // 查询数据 日数据
        List<RegionAirQualityPO> cityDayAirList = mongoTemplateStandardSiteData.find(query,
            RegionAirQualityPO.class, REGION_AIR_DAY + "-" + cityAirParam.getEndTime().substring(0, 4));
        return cityDayAirList;
    }

    @Override
    public RegionAirQualityPO findRegionLiveAQIByRegionCode(String regionCode) {
        Criteria criteria = Criteria.where("REGIONCODE").is(regionCode);
        // 创建查询对象
        Query query = new Query(criteria);

        // 增加排序功能
        query.with(new Sort(Sort.Direction.DESC, "MONITORTIME"));

        Calendar calendar = Calendar.getInstance();

        RegionAirQualityPO cityLiveMap = mongoTemplateStandardSiteData.findOne(query, RegionAirQualityPO.class,
            REGION_AIR_HOUR + "-" + +calendar.get(Calendar.YEAR));
        return cityLiveMap;
    }

    @Override
    public List<RegionAirQualityPO> findRegionHour12DataByRegionCode(String regionCode) {

        Calendar calendar = Calendar.getInstance();
        String tableName = REGION_AIR_HOUR + calendar.get(Calendar.YEAR);

        // 查询条件 增加时间查询条件
        Criteria criteria = Criteria.where("REGIONCODE").is(regionCode);

        // 获取最新记录
        List<RegionAirQualityPO> newDataList = mongoTemplateStandardSiteData.find(
            new Query(criteria).with(new Sort(Sort.Direction.DESC, "MONITORTIME")).limit(12),
            RegionAirQualityPO.class, tableName);

        // 按照时间的正序排序
        Collections.reverse(newDataList);
        return newDataList;
    }
}
